/***
 * Brian Hession -- February 19, 2013
 *
 * Fighter
 *
 * Date			Changes
 * 2/19/2013	Initial version
 *
 */

#include "fighter.h"

Fighter::Fighter() {
	pos.x = 0;
	pos.y = 0;
	image = 0;
	scale = 5;
	vx = 0;
	vy = 0;
}

void Fighter::init() {
	image = Graphics::loadImage( "graphics/skeleton.png" );
	image = Graphics::scaleImage( image, scale, SMOOTHING_OFF );
	SDL_Rect* rects = new SDL_Rect[16];
	for( int i = 0; i < 4; ++i ) {
		for( int j = 0; j < 4; ++j ) {
			rects[i*4+j].x = j*40*scale;
			rects[i*4+j].y = i*40*scale;
			rects[i*4+j].w = 40*scale;
			rects[i*4+j].h = 40*scale;
		}
	}
	a.setImages(rects);
	a.setNumClips(4);
	a.setLength(4);
	a.setClip(standingR);
	a.setDuration(750);
}

void Fighter::calculate( int delta ) {
	a.setTimestamp( a.getTimestamp() + delta );
	if( a.getTimestamp() > a.getDuration()/a.getLength() - 5 ) {
		a.step();
		a.setTimestamp(0);
	}
	pos.x += vx*scale;
	if( pos.x > 600 )
		pos.x = 0;
}

void Fighter::keyPressed( int key ) {
	if( key == this->key.getRight() ) {
		a.setClip( walkingR );
		vx = 1;
	}
	else if( key == this->key.getLeft() ) {
		a.setClip( walkingL );
		vx = -1;
	}
}

void Fighter::keyReleased( int key ) {
	if( key == this->key.getRight() ) {
		a.setClip( standingR );
		vx = 0;
	}
	else if( key == this->key.getLeft() ) {
		a.setClip( standingL );
		vx = 0;
	}
}

